package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Employee;
import cn.wolfcode.query.JsonResult;
import cn.wolfcode.service.IEmployeeService;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller

public class LoginController {
    @Autowired
    private IEmployeeService employeeService;
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("/login")
    @ResponseBody
    public JsonResult login(String username, String password, HttpSession session){
        try {
            Employee employee = employeeService.login(username,password);
            UserContext.setCurrentEmployee(employee);
            if (!employee.isAdmin()){
                UserContext.setExpression(permissionService.queryByEmployeeId(employee.getId()));
            }
            return new JsonResult(true,"登录成功");
        } catch (Exception e) {
            return new JsonResult("登录失败");
        }

    }
}
